package com.zking.web.ly;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.zking.dao.domain.Resume;
import com.zking.service.ly.BiographyService;
import com.zking.utils.RedisUtil;

@Controller
public class BiographyController {
	@Autowired
	private BiographyService bs;
	@Autowired
	private RedisUtil redisUtil;
	
	//未审核查询
	 @RequestMapping("/quesly1")
	    public String quesly1(Resume resume,String state) {
		 List<Resume> ls = bs.que();
		 if (ls != null) {
			 List<Resume> lrs = bs.que2(state);
			 if (redisUtil.lGetListSize("lrs")==0) {
					for (Resume resumes : lrs) {
						redisUtil.lSet("lrs", JSONObject.toJSONString(resumes),120);
					}
					return "redirect:/quesly1s";
				}else {
					return "redirect:/quesly1s";
				}
		}else {
			return "redirect:/quesly1s";
		}
		}
	
	//分页查询
		 @RequestMapping("/quesly1s")
		 public String quesly1s(HttpServletRequest request) {
			 String pnS = request.getParameter("pn");
				
				int pn = 0;
				int limit = 4;
				try{
					pn = Integer.parseInt(pnS);
				} catch (Exception e) {
					pn = 1;
				}
				
				if(pn < 1){
					pn = 1;
				}
				
				int total = (int)redisUtil.lGetListSize("lrs");
				int pages = total / limit;
				if(total % limit != 0){
					pages++;
				}
				
				if(pn > pages){
					pn = pages;
				}
				
				int start = (pn - 1)*limit;//第一个参数
				limit =start+4; 
			 List<Object> list= redisUtil.lGet("lrs", start, limit-1);
			 List<Resume> lrss = new ArrayList<Resume>();
			 for (Object object : list) {
				 lrss.add(JSONObject.parseObject(object.toString(),Resume.class));
			 }
				if (!lrss.isEmpty()) {
					request.setAttribute("lrs", lrss);
					request.setAttribute("pn", pn);
					request.setAttribute("limit", limit);
					request.setAttribute("pages", pages);
					return "canInterviewResumes";
				}else {
					request.setAttribute("msg", "没有查询到数据!");
					return "canInterviewResumes";
				}
		 }
	 
		//合格查询
		 @RequestMapping("/quesly2")
		    public String quesly2(Resume resume,String state) {
				 List<Resume> lrs2 = bs.que2(state);
				 if (redisUtil.lGetListSize("lrs2")==0) {
						for (Resume resumes : lrs2) {
							redisUtil.lSet("lrs2", JSONObject.toJSONString(resumes),120);
						}
						return "redirect:/quesly2s";
					}else {
						return "redirect:/quesly2s";
					}
			}
		
		//分页查询
			 @RequestMapping("/quesly2s")
			 public String quesly2s(HttpServletRequest request) {
				 String pnS = request.getParameter("pn");
					
					int pn = 0;
					int limit = 4;
					try{
						pn = Integer.parseInt(pnS);
					} catch (Exception e) {
						pn = 1;
					}
					
					if(pn < 1){
						pn = 1;
					}
					
					int total = (int)redisUtil.lGetListSize("lrs2");
					int pages = total / limit;
					if(total % limit != 0){
						pages++;
					}
					
					if(pn > pages){
						pn = pages;
					}
					
					int start = (pn - 1)*limit;//第一个参数
					limit =start+4; 
				 List<Object> list= redisUtil.lGet("lrs2", start, limit-1);
				 List<Resume> lrss2 = new ArrayList<Resume>();
				 for (Object object : list) {
					 lrss2.add(JSONObject.parseObject(object.toString(),Resume.class));
				 }
					if (!lrss2.isEmpty()) {
						request.setAttribute("lrs2", lrss2);
						request.setAttribute("pn", pn);
						request.setAttribute("limit", limit);
						request.setAttribute("pages", pages);
						return "haveRefuseResumes";
					}else {
						request.setAttribute("msg", "没有查询到数据!");
						return "haveRefuseResumes";
					}
			 }
			 
			//不合格查询
			 @RequestMapping("/quesly3")
			    public String quesly3(Resume resume,String state) {
					 List<Resume> lrs3 = bs.que2(state);
					 if (redisUtil.lGetListSize("lrs3")==0) {
							for (Resume resumes : lrs3) {
								redisUtil.lSet("lrs3", JSONObject.toJSONString(resumes),120);
							}
							return "redirect:/quesly3s";
						}else {
							return "redirect:/quesly3s";
						}
				}
			
			//分页查询
				 @RequestMapping("/quesly3s")
				 public String quesly3s(HttpServletRequest request) {
					 String pnS = request.getParameter("pn");
						
						int pn = 0;
						int limit = 4;
						try{
							pn = Integer.parseInt(pnS);
						} catch (Exception e) {
							pn = 1;
						}
						
						if(pn < 1){
							pn = 1;
						}
						
						int total = (int)redisUtil.lGetListSize("lrs3");
						int pages = total / limit;
						if(total % limit != 0){
							pages++;
						}
						
						if(pn > pages){
							pn = pages;
						}
						
						int start = (pn - 1)*limit;//第一个参数
						limit =start+4; 
					 List<Object> list= redisUtil.lGet("lrs3", start, limit-1);
					 List<Resume> lrss3 = new ArrayList<Resume>();
					 for (Object object : list) {
						 lrss3.add(JSONObject.parseObject(object.toString(),Resume.class));
					 }
						if (!lrss3.isEmpty()) {
							request.setAttribute("lrs3", lrss3);
							request.setAttribute("pn", pn);
							request.setAttribute("limit", limit);
							request.setAttribute("pages", pages);
							return "haveRefuseResumes2";
						}else {
							request.setAttribute("msg", "没有查询到数据!");
							return "haveRefuseResumes2";
						}
				 }
	//删除			 
	@RequestMapping(value = "/delly")
	public ModelAndView del1(HttpServletRequest request,int user_id,int id,Resume resume) {
		ModelAndView modelAndView = new ModelAndView();
			int j = bs.deletly(resume);
			modelAndView.addObject("j", j);
			modelAndView.setViewName("create");
		return modelAndView;
	}
	
	//修改1
	@RequestMapping(value = "/upsly1")
	public ModelAndView update1(HttpServletRequest request,int user_id,int id,Resume resume,String email) {
		ModelAndView modelAndView = new ModelAndView("redirect:Emails");
			Resume rs = bs.se2(resume);
			if (rs != null) {
				resume.setState("合格");
				int x = bs.upd(resume);
				modelAndView.addObject("email", email);
			} else {
				System.out.println("无数据");
			}
		return modelAndView;
	}
	
	//修改2
	@RequestMapping(value = "/upsly2")
	public ModelAndView update2(HttpServletRequest request,int user_id,int id,Resume resume,String email) {
		ModelAndView modelAndView = new ModelAndView("redirect:Emails2");
			Resume rs = bs.se2(resume);
			if (rs != null) {
				resume.setState("不合格");
				int y = bs.upd(resume);
				modelAndView.addObject("email", email);
			} else {
				System.out.println("无数据");
			}
		return modelAndView;
	}
}
